using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Rapier.Kodachi.Shared;
using System.IO;

namespace Rapier.Kodachi.WebSearch
{
    public partial class Settings : Form
    {
        #region Data Members
        private WebSearchPlugin webSearchPlugin;
        #endregion

        #region Constructor
        public Settings(WebSearchPlugin webSearchPlugin)
        {
            InitializeComponent();
            this.webSearchPlugin = webSearchPlugin;
            UpdateList();
        } 
        #endregion

        #region Private Methods
        private void UpdateList()
        {
            listView1.BeginUpdate();
            listView1.Items.Clear();
            ilEngines.Images.Clear();

            foreach (WebSearchEngine engine in WebSearchPlugin.WebSearchSettings.Engines)
            {
                AddEngineToList(engine);
            }

            listView1.EndUpdate();
        }

        private void AddEngineToList(WebSearchEngine engine)
        {
            Bitmap image = engine.GetImage(webSearchPlugin);
            ilEngines.Images.Add(engine.Name, image);
            ListViewItem engineItem = new ListViewItem(engine.Name, engine.Name);
            engineItem.Tag = engine;
            listView1.Items.Add(engineItem);
        }

        private void AddEngine()
        {
            AddEditSearchEngine aese = new AddEditSearchEngine(webSearchPlugin);
            aese.ShowDialog();
            UpdateList();
        }

        private void EditSelectedEngine()
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            WebSearchEngine engine = listView1.SelectedItems[0].Tag as WebSearchEngine;
            AddEditSearchEngine aese = new AddEditSearchEngine(webSearchPlugin, engine);
            aese.ShowDialog();
            UpdateList();
        }
        
        private void DeleteSelectedEngine()
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            WebSearchEngine engine = listView1.SelectedItems[0].Tag as WebSearchEngine;
            string prompt = "Are you sure you wish to delete the search engine '" + engine + "'?";
            if (MessageBox.Show(prompt, "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            WebSearchPlugin.WebSearchSettings.Engines.Remove(engine);
            UpdateList();
        }
        #endregion

        #region Event Handlers
        private void buttonOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditSelectedEngine();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            DeleteSelectedEngine();
        }

        private void listView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            if (e.KeyCode == Keys.Insert)
                AddEngine();
            else if (e.KeyCode == Keys.Delete)
                DeleteSelectedEngine();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonEdit.Enabled = listView1.SelectedItems.Count > 0;
            buttonDelete.Enabled = listView1.SelectedItems.Count > 0;
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            EditSelectedEngine();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddEngine();
        }
        #endregion
    }
}